<?php
/**
 *	@name		商品拍卖
 *	@author		Jeffery
 *	@qq			1017756891
 *	@email		1017756891@qq.com
 *	@website	http://www.iweitao.net
**/
if(!defined('IN_DISCUZ')) { //安全判断
	exit('Access Denied');
}

//初始化部分数据
$DISCUZ_PAIPAI_VERSION = "7.0"; //定义当前插件版本
if(isset($_GET['action']) && $_GET['action'] == 'plugins'){
	loadcache('plugin');
}

//交易消耗积分类型
$credit = $_G['cache']['plugin']['iweitao_paipai']['credit'];
//允许出售的用户组
$groups = $_G['cache']['plugin']['iweitao_paipai']['groups'];
//商品类型
$types = $_G['cache']['plugin']['iweitao_paipai']['type'];


//首页商品数量
$count = $_G['cache']['plugin']['iweitao_paipai']['count'];
//商品材料
$mades = $_G['cache']['plugin']['iweitao_paipai']['made'];
//管理用户组
$admingroup = $_G['cache']['plugin']['iweitao_paipai']['admingroup'];

//延迟拍卖增加时间(分钟)
$delay_time = $_G['cache']['plugin']['iweitao_paipai']['delay_time'];
$delay_time = $delay_time*60; //转换单位秒
//距离结束多久进入延时拍卖
$delay = $_G['cache']['plugin']['iweitao_paipai']['delay'];
$delay = $delay*60; //转换单位秒
//V认证通讯密钥
$checkv = $_G['cache']['plugin']['iweitao_paipai']['checkv'];

//V认证通讯地址
$checkvurl = $_G['cache']['plugin']['iweitao_paipai']['checkvurl'];
//V认证通讯密钥
$checkvkey = $_G['cache']['plugin']['iweitao_paipai']['checkvkey'];
//将类型转化为数组显示
$types = loadarray($types);
//将商品材料转化为数组显示
$mades = loadarray($mades);
//用户组id
$groups = unserialize($groups);

// 拍卖主题tid
$pp_fid = $_G['cache']['plugin']['iweitao_paipai']['pp_fid'];
/**
 * [notifity 发送普通消息]
 * @param  [type] $uid [用户id]
 * @param  [type] $msg [消息内容]
 * @return [type]      [description]
 */
function notifity($uid,$msg) { //发送消息通知
	global $_G;
	notification_add($uid ,'post', $msg ,array(),$system = 0);
}

/**
 * [authenticate_paipai V认证]
 * @return [type] [description]
 */
function authenticate_paipai() {
	global $checkv;
	global $checkvurl;
	global $checkvkey;
	global $_G;
	$checkvkeymd5 = md5($checkvkey);
	$contents = file_get_contents($checkvurl."?id=authenticate:user&key=".$checkvkey."&md5=".$checkvkeymd5."&uid=".$_G['uid']."&checktype=1");
	$contents = serialize($contents);
	if(!$contents) {
		showmessage("请先进行V认证在发布");
	} else {
		return true;
	}
}

/**
 * [checkcredit 查询积分是否足够]
 * @param  [type] $price [查询的积分]
 * @return [type]        [description]
 */
function checkcredit($price) {
	global $_G;
	global $lang;
	global $credit;
	$query = DB::query("SELECT * FROM `".DB::table("common_member_count")."` WHERE `uid` = '".$_G['uid']."'"); //获取当前用户积分
	while($user_count = mysql_fetch_array($query)) {
		$extcredits = $user_count['extcredits'.$credit];
	}
	if($price >= $extcredits) {
		return false;
	} else {
		return true;
	}
}

/**
 * 商品拍卖核心类
 */
class safe_weitao_plugin_iweitao_paipai {
	/**
	 * [_get description]
	 * @param  [type]  $parameter [需要获得的get参数]
	 * @param  integer $number    [是否为数字值，是则返回数字类型]
	 * @return [type]             [get值]
	 */
	public function _get($parameter,$number = 0) {
		if(!isset($_GET[$parameter])) return null;
		$parameter = $_GET[$parameter];
		if($number == 1) {
			$parameter = $this->number($parameter);
		} else {
			$parameter = $this->grapheme($parameter);
		}
		return $parameter;
	}
	
	/**
	 * [_post 获得post参数]
	 * @param  [type]  $parameter [description]
	 * @param  integer $number    [是否为数字值，是则返回数字类型]
	 * @return [type]             [post值]
	 */
	public function _post($parameter,$number = 0 ) {
		if(!isset($_POST[$parameter])) return null;

		$parameter = $_POST[$parameter];
		if($number == 1) {
			$parameter = $this->number($parameter);
		} else {
			$parameter = $this->grapheme($parameter);
		}
		return $parameter;
	}
	/**
	 * [number 转化数字类型]
	 * @param  [type] $number [description]
	 * @return [type]         [description]
	 */
	public function number($number) {
		$number = intval($number);
		return $number;
	}
	
	/**
	 * [grapheme 转义]
	 * @param  [type] $parameter [description]
	 * @return [type]            [description]
	 */
	public function grapheme($parameter) {
		$parameter = addslashes($parameter);
		return $parameter;
	}
	
	/**
	 * [ungrapheme 反转义]
	 * @param  [type] $parameter [description]
	 * @return [type]            [description]
	 */
	public function ungrapheme($parameter) {
		$parameter = stripslashes($parameter);
		return $parameter;
	}

/**
	 * 获得分页参数
	 * @return [type] [description]
	 * $arr['mulipage'] = $mulipage;	// 起始点
	 * $arr['pagecount'] = $pagecount;	// 每页显示数
	 * $arr['page'] = $page;			// 当前页
	 */
	public function get_page_param($count = 8){
		$arr = array();
		if($this->_get("page")) {
			$page = $this->_get("page");	
			$pagecount = $count;			//每页显示数
			$page = $page - 1;				
			$mulipage = $page*$count;		//当前开始记录
			$page = $page + 1;				//当前页
		} else {
			$mulipage = 0;
			$page = 1;						//当前页
			$pagecount = $page*$count;
		}
		
		$arr['mulipage'] = $mulipage;	// 起始点
		$arr['pagecount'] = $pagecount;	// 每页显示数
		$arr['page'] = $page;			// 当前页

		return $arr;

	}


}
$core = new safe_weitao_plugin_iweitao_paipai();
/**
 * [loadarray 将1=aaa,2=bbb 格式字符串 转化为数组表示]
 * @param  [type] $array [description]
 * @return [array]        [description]
 */
function loadarray($array){
	$return = array();
	$array = explode("\n",str_replace("\r","",$array));
	foreach ($array as $v){
		$t = explode("=",$v);
		$t[0] = trim($t[0]);
		$return[$t[0]] = $t[1];
	}
	return $return;
}

/**
 * [get_left_time 时间描述转换为字符串输出 格式 2014-05-20 15:30]
 * @param  [type]  $time [时间秒数]
 * @param  boolean $sec  [输出是否显示秒]
 * @return [string]        [description]
 */
function get_left_time($time,$sec=true){
	$re = '';
	//天
	$d = 3600*24;
	$day 	= floor($time/$d);
	$re .= $day>0 ? $day.'天 ' : '';
	//小时
	$h = 3600;
	$hour = floor($time%($d)/$h);
	$re .= ' '.$hour . '小时';
	//分
	$m = 60;
	$minute = floor($time%($h)/$m);
	$re .= $minute . '分';
	//秒
	if($sec){
		$second = floor($time%$m);
		$re .= $second . '秒';
	}
	
	return $re;
}

// 数据库操作
/**
 * [db_get_array 获得所有记录]
 * @param  [type] $query_obj [description]
 * @return [type]            [description]
 */
function db_get_array($query_obj){
	$rows = array();
	while($row = mysql_fetch_assoc($query_obj)){
		$rows[] = $row;
	}
	return $rows ? $rows : null;
}

/**
 * [db_get_one 获得一条记录]
 * @param  [type] $query_obj [description]
 * @return [type]            [description]
 */
function db_get_one($query_obj){
	$row = mysql_fetch_assoc($query_obj);
	return $row ? $row : null;
}



?>